package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.CarSelectTemplate;
import com.x.framework.Base;
import com.x.framework.dao.BaseDao;

@Repository
public class CarSelectTemplateDao extends BaseDao {

	
	/**
	 * 
	 * @param carSelectTemplate 封装了查询条件
	 * @return 满足条件的carSelectTemplate列表
	 * @throws Exception
	 */
	private final static String selectCarSelectTemplateListSql = "SELECT " +
			"ecst.* " +
			"FROM ERP_CAR_SELECT_Template ecst " +
			"WHERE 1 = 1 ";
	public List<CarSelectTemplate> selectCarSelectTemplateList(CarSelectTemplate carSelectTemplate) throws Exception{
		StringBuilder sql = new StringBuilder(selectCarSelectTemplateListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carSelectTemplate);
		return this.queryForList(sql.toString(), CarSelectTemplate.class, params);
	}

	/**
	 * 
	 * @param carSelectTemplate 封装了查询条件
	 * @return 满足条件的carSelectTemplate列表
	 * @throws Exception
	 */
	private final static String selectCarSelectTemplateListByGroupSql = "SELECT " +
			"ecst.Template_KEY, " +
			"GROUP_CONCAT(ecst.Template_VALUE) AS 'Template_VALUES' " +
			"FROM ERP_CAR_SELECT_Template ecst " +
			"WHERE 1 = 1 ";
	public List<CarSelectTemplate> selectCarSelectTemplateListByGroup(CarSelectTemplate carSelectTemplate) throws Exception{
		StringBuilder sql = new StringBuilder(selectCarSelectTemplateListByGroupSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carSelectTemplate);
		sql.append("GROUP BY ecst.Template_KEY ");
		return this.queryForList(sql.toString(), CarSelectTemplate.class, params);
	}
	//拼接查询条件
	private void searchCondition(StringBuilder sql, List<Object> params, CarSelectTemplate carSelectTemplate) {
		if(carSelectTemplate != null){
			if(this.isString(carSelectTemplate.getTemplateId())){
				sql.append("AND ecst.Template_ID = ? ");
				params.add(carSelectTemplate.getTemplateId());
			}
			if(this.isString(carSelectTemplate.getMarketId())){
				sql.append("AND ecst.MARKET_ID = ? ");
				params.add(carSelectTemplate.getMarketId());
			}
			if(this.isString(carSelectTemplate.getShopId())){
				sql.append("AND ecst.SHOP_ID = ? ");
				params.add(carSelectTemplate.getShopId());
			}
			if(this.isString(carSelectTemplate.getUserId())){
				sql.append("AND ecst.USER_ID = ? ");
				params.add(carSelectTemplate.getUserId());
			}
		}
	}
	/**
	 * 
	 * @param list 封装了待插入的集合
	 * @return 
	 * @throws Exception
	 */
	private final static String insertCarSelectTemplateSql = "INSERT INTO " +
			"ERP_CAR_SELECT_TEMPLATE " +
			"(TEMPLATE_ID,TEMPLATE_KEY,TEMPLATE_VALUE,TEMPLATE_REMARK,MARKET_ID,SHOP_ID,USER_ID,TEMPLATE_CREATE_TIME) " +
			"VALUES ";
	public void insertCarSelectTemplate(CarSelectTemplate carSelectTemplate, List<CarSelectTemplate> list) {
		if(list.size() == 0 ){
			return;
		}else{
			StringBuilder sql = new StringBuilder(insertCarSelectTemplateSql);
			List<Object> params = new ArrayList<Object>();
			insertSqlAppend(sql, params, list);
			this.execute(sql.toString(), params);
		}
	}
	private void insertSqlAppend(StringBuilder sql, List<Object> params, List<CarSelectTemplate> list) {
		for ( int i = 0; i < list.size(); i++) {
			if(i == list.size()-1){
				sql.append("(?,?,?,?,?,?,?,?) ");
			}else{
				sql.append("(?,?,?,?,?,?,?,?), ");
			}
			CarSelectTemplate temp = list.get(i);
			params.add(Base.getUid(32));
			params.add(temp.getTemplateKey());
			params.add(temp.getTemplateValue());
			params.add(temp.getTemplateRemark());
			params.add(temp.getMarketId());
			params.add(temp.getShopId());
			params.add(temp.getUserId());
			params.add(temp.getTemplateCreateTime());
		}
	}

}
